<template>
  <div id="recharge">
    <mt-header title="我要充值" fixed style="background-color: #24aa7d;color:#fff;">
      <router-link to="/me" slot="left">
        <mt-button icon="back"></mt-button>
      </router-link>
    </mt-header>

    <div id="cz">
      <p class="cz_tit">充值金额</p>
      <mt-field class="monry" label="￥" v-model="money"></mt-field>
    </div>
    <mt-button class="btn" type="primary" @click="btn_account">立即充值</mt-button>
  </div>
</template>


<script>
import https from "../../utils/http.js";
export default {
  props: {},
  data() {
    return {
      money: "0",
      token: "00DF007E004C006100710011006100D000F3006F006E00E000BD0038009700AE"
    };
  },
  computed: {},
  created() {
    this.token = localStorage.getItem("token");
  },
  mounted() {},
  watch: {},
  methods: {
    btn_account() {
      var that = this;
      var a = /^\+?[1-9][0-9]*$/;
      if (this.money == "" || this.money == "0") {
        Toast("请输入充值金额");
        return false;
      }
      if (!a.test(this.money)) {
        Toast("充值金额必须为正整数");
        return false;
      }
      Indicator.open({
        text: "加载中...",
        spinnerType: "fading-circle"
      });
      https({
        method: "post",
        url: "/api/WxPay/PubPay",
        data: {
          TokenCode: this.token,
          VersionCode: "1.0",
          TimeStamp: 0,
          FormData: {},
          RecordKey: this.money,
          SignCode: ""
        }
      })
        .then(res => {
          if (res.ResultCode == 111111) {
            this.wx_value = JSON.parse(res.ResultValue);
            Indicator.close();
            WeixinJSBridge.invoke(
              "getBrandWCPayRequest",
              {
                appId: this.wx_value.appId, //公众号名称，由商户传入
                timeStamp: this.wx_value.timeStamp, //时间戳，自1970年以来的秒数
                nonceStr: this.wx_value.nonceStr, //随机串
                package: this.wx_value.package,
                signType: this.wx_value.signType, //微信签名方式：
                paySign: this.wx_value.paySign //微信签名
              },
              function(res) {
                if (res.err_msg == "get_brand_wcpay_request:ok") {
                  Toast("充值成功");

                  that.$router.push("/account");
                } else {
                  Toast("充值失败");
                }
              }
            );
          } else {
            Toast(res.ResultDescribe);
            Indicator.close();
          }
        })
        .catch(err => {
          Indicator.close();
          console.log(err);
        });
    }
  },
  components: {}
};
</script>

<style scoped lang="less">
.title {
  width: 95%;
  height: 100px;
  line-height: 100px;
  padding-left: 5%;
  background: #eee;
}
.cz_tit {
  width: 98%;
  height: 6rem;
  padding-left: 5%;
  padding-top: 2rem;
  font-size: 1.2rem;
  font-weight: 800;
  box-sizing: border-box;
}
.money {
  width: 98%;

  line-height: 40px;
  padding-left: 5%;
  padding-top: 1.5rem;
  font-size: 1.6rem;
  font-weight: 800;
  color: red;
  box-sizing: border-box;
}
.btn {
  width: 80%;
  height: 80px;
  margin-left: 10%;
  margin-top: 3rem;
  background: #24aa7d;
}
</style>
